
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include "processlib.h"
#include "Stu.h"

#include "tlhelp32.h"

HANDLE GetProcessHandle(DWORD dwPid) {

	HANDLE hProess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid);

	return hProess;
}

BOOL IsExistProcess(CONST CHAR* szProcessName,OUT DWORD& dwPID)
{
	PROCESSENTRY32 processEntry32;
	HANDLE toolHelp32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (((int)toolHelp32Snapshot) != -1)
	{
		processEntry32.dwSize = sizeof(processEntry32);
		if (Process32First(toolHelp32Snapshot, &processEntry32))
		{
			do
			{
				if (strcmp(szProcessName, processEntry32.szExeFile) == 0)
				{
					dwPID = processEntry32.th32ProcessID;
					return TRUE;
				}
			} while (Process32Next(toolHelp32Snapshot, &processEntry32));
		}
		CloseHandle(toolHelp32Snapshot);
	}

	return FALSE;
}

BaseMoudleInfos ListProcessModules(DWORD dwPID)
{
	BaseMoudleInfos infos;
	HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
	MODULEENTRY32 me32;

	//  Take a snapshot of all modules in the specified process. 
	hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
	if (hModuleSnap == INVALID_HANDLE_VALUE)
	{

		return infos;
	}

	//  Set the size of the structure before using it. 
	me32.dwSize = sizeof(MODULEENTRY32);

	//  Retrieve information about the first module, 
	//  and exit if unsuccessful 
	if (!Module32First(hModuleSnap, &me32))
	{
		CloseHandle(hModuleSnap);     // Must clean up the snapshot object! 
		return infos;
	}

	//  Now walk the module list of the process, 
	//  and display information about each module 
	do
	{
		BaseMoudleInfo info;
		info.dwImageSize = me32.modBaseSize;
		info.dwModbase = (DWORD)me32.modBaseAddr;
		info.modname = me32.szModule;
		info.modpath = me32.szExePath;
		infos.push_back(info);

		//OutputDebugPrintf(TEXT("\n  [fuck]   MODULE NAME:     %s"), me32.szModule);
		//OutputDebugPrintf(TEXT("\n  [fuck]   executable     = %s"), me32.szExePath);
		//OutputDebugPrintf(TEXT("\n  [fuck]   process ID     = 0x%08X"), me32.th32ProcessID);
		//OutputDebugPrintf(TEXT("\n  [fuck]   ref count (g)  =     0x%04X"), me32.GlblcntUsage);
		//OutputDebugPrintf(TEXT("\n  [fuck]   ref count (p)  =     0x%04X"), me32.ProccntUsage);
		//OutputDebugPrintf(TEXT("\n  [fuck]   base address   = 0x%08X"), (DWORD)me32.modBaseAddr);
		//OutputDebugPrintf(TEXT("\n  [fuck]   base size      = 0x%08X"), me32.modBaseSize);

	} while (Module32Next(hModuleSnap, &me32));


	//  Do not forget to clean up the snapshot object. 
	CloseHandle(hModuleSnap);
	return infos;
}


byte* GetMainModBase(DWORD dwPID) {

	HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
	MODULEENTRY32 me32;

	//  Take a snapshot of all modules in the specified process. 
	hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
	if (hModuleSnap == INVALID_HANDLE_VALUE)
	{

		return(FALSE);
	}

	//  Set the size of the structure before using it. 
	me32.dwSize = sizeof(MODULEENTRY32);

	//  Retrieve information about the first module, 
	//  and exit if unsuccessful 
	if (!Module32First(hModuleSnap, &me32))
	{
		CloseHandle(hModuleSnap);     // Must clean up the snapshot object! 
		return(FALSE);
	}

	//  Now walk the module list of the process, 
	//  and display information about each module 
	do
	{
		std::string strname = me32.szModule;

		if (strname.find(".exe") != std::string::npos) {

			return me32.modBaseAddr;
		}

		//OutputDebugPrintf(TEXT("\n  [fuck]   MODULE NAME:     %s"), me32.szModule);
		//OutputDebugPrintf(TEXT("\n  [fuck]   executable     = %s"), me32.szExePath);
		//OutputDebugPrintf(TEXT("\n  [fuck]   process ID     = 0x%08X"), me32.th32ProcessID);
		//OutputDebugPrintf(TEXT("\n  [fuck]   ref count (g)  =     0x%04X"), me32.GlblcntUsage);
		//OutputDebugPrintf(TEXT("\n  [fuck]   ref count (p)  =     0x%04X"), me32.ProccntUsage);
		//OutputDebugPrintf(TEXT("\n  [fuck]   base address   = 0x%08X"), (DWORD)me32.modBaseAddr);
		//OutputDebugPrintf(TEXT("\n  [fuck]   base size      = %d"), me32.modBaseSize);

	} while (Module32Next(hModuleSnap, &me32));


	//  Do not forget to clean up the snapshot object. 
	CloseHandle(hModuleSnap);
	return 0;

}


